﻿<template>
	<div >
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
					<el-form :model="queryParams" ref="queryForm" :inline="true">
						<el-row :gutter="35">
			              <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
			              <el-form-item label="设备编码">
			              <el-input v-model="queryParams.groupNum" clearable placeholder="设备编码"/>
			              </el-form-item>
			              </el-col>
						  <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
			              <el-form-item label="站台号">
			              <el-input v-model="queryParams.unitNum" clearable placeholder="站台号或PLC编码"/>
			              </el-form-item>
			              </el-col>
                
			          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" class="mb20 search-actions">
         
			          <div>
						<el-button v-if="multipleSelection.length>0" icon="ele-Close" @click="handleStopAll" v-auth="'bcrGroup:control'"> 停止 </el-button>
						<el-button v-if="multipleSelection.length>0" icon="ele-Check" @click="handleConnectAll" v-auth="'bcrGroup:control'"> 重连 </el-button>
			          <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
			          <el-button type="primary" plain icon="ele-Search" @click="handleQuery" v-auth="'plcGroup:page'"> 查询 </el-button>
			          </div>
			          </el-col>
			      </el-row>
			      </el-form>
	   </el-card>
		<el-card shadow="hover" style="margin-top: 8px">
			<el-table
				:data="tableData"
				@selection-change="handleSelectionChange"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
				row-key="id"
				border="">
				<el-table-column type="selection" width="55" />
				<el-table-column type="index" label="序号" width="55" align="center" fixed="" />
				 <el-table-column prop="clientID" label="客户端id" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="num" label="设备编码" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="unitNum" label="站台号" fixed="" show-overflow-tooltip="" 
					/>
				 <el-table-column prop="name" label="设备名称" fixed="" show-overflow-tooltip="" 
					/>
					<el-table-column prop="plcName" label="Plc编号" fixed="" show-overflow-tooltip="" 
					/>
				 <el-table-column prop="ipAddress" label="IP" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="port" label="端口" fixed="" show-overflow-tooltip="" />

				 <el-table-column prop="layerNumber" label="扫码枪层数" fixed="" show-overflow-tooltip="" 
					/>
				
					<el-table-column prop="readBCR1Layer" label="托盘号" fixed="" show-overflow-tooltip="" 
					/>
					<el-table-column prop="readStartTime" label="读取时间" fixed="" show-overflow-tooltip="" 
					/>
					<el-table-column prop="receiveEndTime" label="读取结束时间" fixed="" show-overflow-tooltip="" 
					/>
				  <el-table-column  label="是否连接"  :filters="[
				  	{ text: '已连接', value: true },
				  	{ text: '未连接', value: false },
				  ]"
	      :filter-method="filterConnect"
	      filter-placement="bottom-end">
				      <template #default="scope">
				        <el-tag
				          :type="scope.row.connect === true ? 'success' : 'danger'"
				          disable-transitions >{{ scope.row.connect == true ? '已连接' : '未连接' }}
						</el-tag>
				      </template>
				    </el-table-column>
				<el-table-column  label="是否监听" >
			      <template #default="scope">
			        <el-tag
			          :type="scope.row.status === true ? 'success' : 'danger'"
			          disable-transitions >{{ scope.row.status == true ? '监听中' : '未监听' }}
					</el-tag>
			      </template>
			    </el-table-column>
				  
				<el-table-column label="操作" width="180" align="center" fixed="right" show-overflow-tooltip="" >
					<template #default="scope">
						<el-button  size="small" text="" type="primary" @click="stopControl(scope.row)" v-auth="'bcrGroup:control'"> 停止 </el-button>
						<el-button  size="small" text="" type="primary" @click="connectControl(scope.row)" v-auth="'bcrGroup:control'"> 重连 </el-button>
						<el-button  size="small" text="" type="primary" @click="callBcrRead(scope.row)" v-auth="'bcrGroup:control'">  扫码</el-button>
					</template>
				</el-table-column>
			</el-table>
			
			
		</el-card>
	</div>
</template>

<script lang="ts" setup>
	import { ref } from "vue";
	import { ElMessageBox, ElMessage } from "element-plus";
	import { auth } from '/@/utils/authFunction';
	//import { formatDate } from '/@/utils/formatTime';

	
	import { GetBcrGroup,ControlEquip,ReadBcr,isNumber } from '/@/api/main/grpcServer';


		
		const loading = ref(false);
		const tableData = ref<any>([]);
        const queryParams = ref<any>(
		{	
			
		});
	
	const tableParams = ref({
	page: 1,
	pageSize: 10,
	total: 0,
	});

	const multipleSelection = ref<any>([])

	const handleSelectionChange = (val: []) => {
	multipleSelection.value = val
    }

	// 查询操作
	const handleQuery = async () => {
	loading.value = true;
	let para={
		groupNum:'0',
		unitNum:'0'		
		};
	if(queryParams.value){
		
		para=JSON.parse(JSON.stringify(queryParams.value));
		if(!queryParams.value.groupNum){
			para.groupNum='0';
		}
		else{
			if (!isNumber(para.groupNum)) {
				ElMessage({
					message: '编码必须为数字.',
					type: 'error',
				});
				loading.value = false;
				return;

			}
		}
		if(!queryParams.value.unitNum){
			para.unitNum='0';
		}
	}
	
	var res = await GetBcrGroup(para);
	let datas:any[]=[];

	datas=res.data.result;


	if(datas && para.unitNum!='0'){
		datas=datas.filter((x)=>{
			return x.unitNum==para.unitNum || x.plcName==para.unitNum;
		})
	}
	tableData.value = datas;
	tableParams.value.total = res.data.result?.length;
	loading.value = false;
};



const filterConnect = (value: string, row: any) => {
  return row.connect==value;
}
 
const stopControl= async(row:any)=>{

	loading.value=true;
    let data={
		informType:2,
		message:row.num,
		clientID:row.clientID,
		deviceType:3
	}
    await ControlEquip(data);
	loading.value=false;
	ElMessage({
    message: '操作成功.',
    type: 'success',
  });
 
};

const connectControl=async(row:any)=>{

	loading.value=true;
	let data={
		informType:1,
		message:row.num,
		clientID:row.clientID,
		deviceType:3
	}
	await ControlEquip(data);
	loading.value=false;
	ElMessage({
    message: '操作成功.',
    type: 'success',
  });



  
 
};

const callBcrRead=async(row:any)=>{
	let data={
		groupNum:row.num
	};
	await ReadBcr(data);


	ElMessage({
    message: '操作成功.',
    type: 'success',
  });
  };

const handleStopAll = async () => {

	loading.value = true;
	multipleSelection.value.forEach(async (row: any) => {
		let data = {
			informType: 2,
			message: row.num,
			clientID: row.clientID,
			deviceType: 3
		}
		await ControlEquip(data);
	});

	loading.value = false;
	ElMessage({
		message: '操作成功.',
		type: 'success',
	});
}

const handleConnectAll = async () => {

	loading.value = true;

	multipleSelection.value.forEach(async (row: any) => {
		let data = {
			informType: 1,
			message: row.num,
			clientID: row.clientID,
			deviceType: 3
		}
		await ControlEquip(data);
	});
	loading.value = false;
	ElMessage({
		message: '操作成功.',
		type: 'success',
	});
}



handleQuery();
</script>


